package com.tencent.protocol.unifiedorder_protocol;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.tencent.common.Configure;
import com.tencent.common.RandomStringGenerator;
import com.tencent.common.Signature;

/**
 * 统一下单请求参数
 * @author wuhao
 * date 2016-07-01
 * 接口详情见：
 * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
 */
public class UnifiedorderReqData {

	private String appid = ""; // 应用ID
	private String mch_id = ""; // 商户号
	private String device_info = "WEB"; // 设备号，默认请传"WEB"
	private String nonce_str = ""; // 随机字符串
	private String sign = ""; // 签名
	private String body = ""; // 商品描述
	private String detail = ""; // 商品详情
	private String attach = ""; // 附加数据
	private String out_trade_no = ""; // 商户订单号
	private String fee_type = "CNY"; // 货币类型
	private int total_fee = 0; // 总金额，单位为分
	private String spbill_create_ip = ""; // 终端IP
	private String time_start = ""; // 交易起始时间
	private String time_expire = ""; // 交易结束时间，最短失效时间间隔必须大于5分钟！
	private String goods_tag = ""; // 商品标记
	private String notify_url = ""; // 通知地址
	private String trade_type = ""; // 交易类型
	private String limit_pay = ""; // 指定支付方式，no_credit指定不能使用信用卡支付
	private String product_id =""; // 商品ID
	private String openid = ""; // 用户标识
	private String scene_info = ""; // 用户标识
	
	/**
	 * 统一下单
	 * @param body 商品或支付单简要描述
	 * @param out_trade_no 商户系统内部的订单号,32个字符内、可包含字母
	 * @param total_fee 订单总金额，单位为分
	 * @param spbill_create_ip 用户端实际ip
	 * @param notify_url 接收微信支付异步通知回调地址，通知url必须为直接可访问的url，不能携带参数
	 * @param trade_type 支付类型
	 */
	public UnifiedorderReqData(String body,
			String out_trade_no, int total_fee, String spbill_create_ip, String notify_url,
			String trade_type) {
		
		// 微信分配的公众号ID（开通公众号之后可以获取到）
		setAppid(Configure.getAppid());

		// 微信支付分配的商户号ID（开通公众号的微信支付功能之后可以获取到）
		setMch_id(Configure.getMchid());
		
		// 随机字符串，不长于32 位
		setNonce_str(RandomStringGenerator.getRandomStringByLength(32));

		setBody(body);
		setOut_trade_no(out_trade_no);
		setTotal_fee(total_fee);
		setSpbill_create_ip(spbill_create_ip);
		setNotify_url(notify_url);
		setTrade_type(trade_type);
		
		// 根据API给的签名规则进行签名
		String sign = Signature.getSign(toMap());
		setSign(sign);// 把签名数据设置到Sign这个属性中
	}
	public String getAppid() {
		return appid;
	}
	public void setAppid(String appid) {
		this.appid = appid;
	}
	
	public String getScene_info() {
		return scene_info;
	}
	public void setScene_info(String scene_info) {
		this.scene_info = scene_info;
	}
	public String getMch_id() {
		return mch_id;
	}
	public void setMch_id(String mch_id) {
		this.mch_id = mch_id;
	}
	public String getDevice_info() {
		return device_info;
	}
	public void setDevice_info(String device_info) {
		this.device_info = device_info;
	}
	public String getNonce_str() {
		return nonce_str;
	}
	public void setNonce_str(String nonce_str) {
		this.nonce_str = nonce_str;
	}
	public String getSign() {
		return sign;
	}
	public void setSign(String sign) {
		this.sign = sign;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	public String getAttach() {
		return attach;
	}
	public void setAttach(String attach) {
		this.attach = attach;
	}
	public String getOut_trade_no() {
		return out_trade_no;
	}
	public void setOut_trade_no(String out_trade_no) {
		this.out_trade_no = out_trade_no;
	}
	public String getFee_type() {
		return fee_type;
	}
	public void setFee_type(String fee_type) {
		this.fee_type = fee_type;
	}
	public int getTotal_fee() {
		return total_fee;
	}
	public void setTotal_fee(int total_fee) {
		this.total_fee = total_fee;
	}
	public String getSpbill_create_ip() {
		return spbill_create_ip;
	}
	public void setSpbill_create_ip(String spbill_create_ip) {
		this.spbill_create_ip = spbill_create_ip;
	}
	public String getTime_start() {
		return time_start;
	}
	public void setTime_start(String time_start) {
		this.time_start = time_start;
	}
	public String getTime_expire() {
		return time_expire;
	}
	public void setTime_expire(String time_expire) {
		this.time_expire = time_expire;
	}
	public String getGoods_tag() {
		return goods_tag;
	}
	public void setGoods_tag(String goods_tag) {
		this.goods_tag = goods_tag;
	}
	public String getNotify_url() {
		return notify_url;
	}
	public void setNotify_url(String notify_url) {
		this.notify_url = notify_url;
	}
	public String getTrade_type() {
		return trade_type;
	}
	public void setTrade_type(String trade_type) {
		this.trade_type = trade_type;
	}
	public String getLimit_pay() {
		return limit_pay;
	}
	public void setLimit_pay(String limit_pay) {
		this.limit_pay = limit_pay;
	}
	public String getOpenid() {
		return openid;
	}
	public void setOpenid(String openid) {
		this.openid = openid;
	}
	public String getProduct_id() {
		return product_id;
	}
	public void setProduct_id(String product_id) {
		this.product_id = product_id;
	}
	
	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		Field[] fields = this.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object obj;
			try {
				obj = field.get(this);
				if (obj != null) {
					map.put(field.getName(), obj);
				}
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		return map;
	}

}
